HaskellのPattern Matching
内部的にはcase式のみ
以下に示すものは全てcase式の糖衣構文
関数を複数回行うもの
code:hs
describeList' :: a -> String describeList' [] = "The list is empty."
describeList' x = "The list has one element." describeList' _ = "The list has multiple elements."
データの構造を見て分岐する時に使う
code:hs
describeListWithGuard :: a -> String describeListWithGuard xs
| length xs == 0 = "The list is empty."
| length xs == 1 = "The list has one element."
| otherwise = "The list has multiple elements."
データの性質で分岐する時に使う
otherwiseで網羅性をチェックする
if式を使ったもの
code:hs
isEmptyIf xs = if null xs then True else False
条件分岐が少数の場合に使う
case..ofを使ったもの
code:hs
describeList :: a -> String describeList xs = case xs of
[] -> "The list is empty."
x -> "The list has one element." _ -> "The list has multiple elements."
これだけ->を使うのが紛らわしいmrsekut.icon
定義